package com.asdan.seededu.service.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.asdan.seededu.Calenda;
import com.asdan.seededu.alipay.config.AlipayConfig;
import com.asdan.seededu.alipay.util.AlipaySubmit;
import com.asdan.seededu.entity.security.Order;
import com.asdan.seededu.entity.security.School;
import com.asdan.seededu.entity.security.SubProject;
import com.asdan.seededu.entity.security.User;
import com.asdan.seededu.service.AlipayService;
import com.asdan.seededu.service.OrderService;
import com.asdan.seededu.util.DatabaseAccess;
import com.asdan.seededu.util.EasyMapsManager;

public class OrderServiceImpl extends UtilImpl implements OrderService{

	public User getOrderInfoByID(int ID,int spid,int fpid){

		DatabaseAccess databaseAccess = new DatabaseAccess(false);
		Connection cn = null;
		PreparedStatement pstmt=null;
		ResultSet rs = null;
		User stu = null ;
		String sql = "select uid, name , schoolname , grade ,email ,cellPhone ,QQ ,wchart ,weibo ," +
				"screatTime,sex,birthday,firstName,lastName,idCard,passid,passSignTime,passCloseTime" +
				",address,postCode,fatherName,fatherPhone,fatherEmail,motherName,motherPhone,motherEmail from user  where (uid = ?) AND ifdelete = 0  ";
		System.out.println(sql);
		try{
			cn = databaseAccess.getConn();
			pstmt = cn.prepareStatement(sql);
			pstmt.setInt(1,ID ) ;
			rs = pstmt.executeQuery();
			while(rs.next()){
				stu= new User();
				stu.setUid(rs.getInt("uid")) ;
				stu.setName(rs.getString("name")) ;
				stu.setSchoolname(rs.getString("schoolname")) ;
				stu.setGrade(rs.getString("grade")) ;
				stu.setEmail(rs.getString("email")) ;
				stu.setCellPhone(rs.getString("cellPhone")) ;
				stu.setQQ(rs.getString("qq")) ;
				stu.setWchart(rs.getString("wchart")) ;
				stu.setWeibo(rs.getString("weibo")) ;
				stu.setScreatTime(rs.getString("screatTime")) ;
				stu.setSex(rs.getString("sex")) ; 
				stu.setBirthday(rs.getString("birthday")) ;
				stu.setFirstName(rs.getString("firstName")) ;
				stu.setLastName(rs.getString("lastName")) ;
				stu.setIdCard(rs.getString("idCard")) ;
				stu.setPassid(rs.getString("passid")) ;
				stu.setPassSignTime(rs.getString("passSignTime")) ;
				stu.setPassCloseTime(rs.getString("passCloseTime")) ;
				stu.setAddress(rs.getString("address")) ;
				stu.setPostCode(rs.getString("postCode")) ;
				stu.setFatherName(rs.getString("fatherName")) ;
				stu.setFatherPhone(rs.getString("fatherPhone")) ;
				stu.setFatherEmail(rs.getString("fatherEmail")) ;
				stu.setMotherName(rs.getString("motherName")) ;
				stu.setMotherPhone(rs.getString("motherPhone")) ;
				stu.setMotherEmail(rs.getString("motherEmail")) ;
			}
		}catch(Exception e){
			e.printStackTrace();
			System.out.println(e.getMessage());
		}finally{                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        
			try{
				pstmt.close();
				cn.close();
				databaseAccess.close() ;
			}catch(Exception ex){
				pstmt = null ;
				cn = null ;
			}
		}
		return stu ;
	}
	public boolean getPriceById(int subid,int fpid,String price){

		DatabaseAccess databaseAccess = new DatabaseAccess(false);
		Connection cn = null;
		PreparedStatement pstmt=null;
		ResultSet rs = null;
		boolean result = false;
		String sql = "select price from subproject where spid=? and fpid=?";
		System.out.println("price---------"+price);
		try{
			cn = databaseAccess.getConn();
			pstmt = cn.prepareStatement(sql);
			pstmt.setInt(1, subid);
			pstmt.setInt(2,fpid ) ;
			rs = pstmt.executeQuery();
			if(rs.next())
			{
				if(rs.getString("price")==price)
				{
					result = true;

				}
				else{
					result = true;

				}

			}
		}catch(Exception e){
			e.printStackTrace();
			System.out.println(e.getMessage());
			result = false;
		}finally{                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        
			try{
				pstmt.close();
				cn.close();
				databaseAccess.close() ;
			}catch(Exception ex){
				pstmt = null ;
				cn = null ;
				result = false;
			}
		}
		return result;
	}

	public boolean addOrders(int uid,int spid,int fpid,String price,String information)
	{
		boolean  temp = false ;
		DatabaseAccess databaseAccess = new DatabaseAccess(false);
		Connection cn = null;
		PreparedStatement pstmt=null;
		String sql="insert into orders(SUID , SPID , OCREATTIME , PRICE , OSITUATION , INFORMATION) values(?,?,?,?,0,?) ";
		try{
			String datetime=Calenda.getyyyymmdd();
			cn = databaseAccess.getConn();
			pstmt = cn.prepareStatement(sql);
			pstmt.setInt(1, uid);
			pstmt.setInt(2, spid) ;
			pstmt.setString(3,datetime ) ;
			pstmt.setString(4, price) ;
			pstmt.setString(5, information) ;
			pstmt.execute();
			temp = true ;
		}catch(Exception e){
			e.printStackTrace();
			System.out.println(e.getMessage());

		}finally{                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        
			try{
				pstmt.close();
				cn.close();
				databaseAccess.close() ;
			}catch(Exception ex){
				pstmt = null ;
				cn = null ;
			}
		}
		return temp;
	}

	public boolean updateUser(User stu,int uid) {
		boolean temp = false ;
		DatabaseAccess databaseAccess = new DatabaseAccess(false);
		Connection cn = null;
		PreparedStatement pstmt=null;
		String sql = "update user  set  name=? ,sex=? , grade=?, " +
				"email=? , QQ=? , cellPhone=? , " +
				"firstName=?,lastName=?,"+
				"passid=?,passCloseTime=?, "+
				"fatherName=?,fatherPhone=?,fatherEmail=?,"+
				"motherName=?,motherPhone=?,motherEmail=?  where uid= ?" ;
		System.out.println(sql) ;
		try {
			cn = databaseAccess.getConn() ;
			pstmt = cn.prepareStatement(sql) ;
			pstmt.setString(1,stu.getName()) ;
			pstmt.setString(2, stu.getSex()) ;
			pstmt.setString(3, stu.getGrade()) ;
			pstmt.setString(4, stu.getEmail()) ;
			pstmt.setString(5, stu.getQQ()) ;
			pstmt.setString(6, stu.getCellPhone()) ;
			pstmt.setString(7, stu.getFirstName()) ;
			pstmt.setString(8, stu.getLastName()) ;
			pstmt.setString( 9,stu.getPassid()) ;
			pstmt.setString(10,stu.getPassCloseTime()) ;
			pstmt.setString(11, stu.getFatherName() ) ;
			pstmt.setString( 12,stu.getFatherPhone()) ;
			pstmt.setString(13, stu.getFatherEmail() ) ;
			pstmt.setString( 14,stu.getMotherName()) ;
			pstmt.setString(15, stu.getMotherPhone() ) ;
			pstmt.setString( 16,stu.getMotherEmail()) ;
			pstmt.setInt(17,uid) ;

			pstmt.executeUpdate() ;
			temp = true ;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			try {
				pstmt.close() ;
				cn.close() ;
				databaseAccess.close() ;
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return temp;
	}

	@SuppressWarnings("deprecation")
	public String AddAlipay(String ordersid,String uid, String out_trade_no, String subject,
			String price, String body, String show_url, String receive_name,
			String receive_address, String receive_zip, String receive_phone,
			String receive_mobile) {
		// TODO Auto-generated method stub
		try{
			Calenda cal = new Calenda();
			out_trade_no = String.valueOf(cal.getNowTimeAli().getYear()) + String.valueOf(cal.getNowTimeAli().getMonth())+String.valueOf(cal.getNowTimeAli().getDate())+
					String.valueOf(cal.getNowTimeAli().getHours()) + String.valueOf(cal.getNowTimeAli().getMinutes()) + String.valueOf(cal.getNowTimeAli().getSeconds())+
					out_trade_no;
			AlipayService as = new AlipayServiceImpl();
			String quantity = "1";
			String logistics_fee = "0.00";
			String logistics_type = "EXPRESS";
			String logistics_payment = "SELLER_PAY";
			String orderid="1";
			if(as.SelectOrdersId(uid,ordersid)==null)
			{
				if(as.InsertOrder(ordersid,uid, out_trade_no, subject, price, body, show_url, receive_name, receive_address, receive_zip, receive_phone, receive_mobile, cal.getNowTimeAli().toString()))
				{
					orderid = as.SelectOrderId(uid, out_trade_no);
				}
			}
			else
			{
				if(as.updateOrder(uid, ordersid, out_trade_no, cal.getNowTimeAli().toString()))
				{
					orderid = as.SelectOrderId(uid, out_trade_no);
				}
			}
			Map<String, String> sParaTemp = new HashMap<String, String>();
			sParaTemp.put("service", "create_partner_trade_by_buyer");
			sParaTemp.put("partner", AlipayConfig.partner);
			sParaTemp.put("seller_email", AlipayConfig.seller_email);
			sParaTemp.put("_input_charset", AlipayConfig.input_charset);
			sParaTemp.put("payment_type", AlipayConfig.payment_type);
			sParaTemp.put("notify_url", AlipayConfig.notify_url+"?oid="+orderid);
			sParaTemp.put("return_url", AlipayConfig.return_url+"?oid="+orderid);
			sParaTemp.put("out_trade_no", out_trade_no);
			sParaTemp.put("subject", subject);
			sParaTemp.put("price", price);
			sParaTemp.put("quantity", quantity);
			sParaTemp.put("logistics_fee", logistics_fee);
			sParaTemp.put("logistics_type", logistics_type);
			sParaTemp.put("logistics_payment", logistics_payment);
			sParaTemp.put("body", body);
			sParaTemp.put("show_url", show_url);
			sParaTemp.put("receive_name", receive_name);
			sParaTemp.put("receive_address", receive_address);
			sParaTemp.put("receive_zip", receive_zip);
			sParaTemp.put("receive_phone", receive_phone);
			sParaTemp.put("receive_mobile", receive_mobile);
			//建立请求
			String sHtmlText = AlipaySubmit.buildRequest(sParaTemp,"get","确认");
			return sHtmlText;
		}catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		return null;
	}
	public boolean updateUser(Order stu, int uid) {
		// TODO Auto-generated method stub
		return false;
	}

	public String getOrdersId(int suid, int spid) {
		// TODO Auto-generated method stub
		String sql = "select o.`OID` from `orders` o where o.SUID=? and o.SPID=? order by o.OID desc;";
		try{
			DatabaseAccess databaseAccess = new DatabaseAccess(false);
			EasyMapsManager easyMapsManager = new EasyMapsManager(databaseAccess);
			easyMapsManager.setPreparedParameter(suid);
			easyMapsManager.setPreparedParameter(spid);
			@SuppressWarnings("rawtypes")
			ArrayList<HashMap> list = easyMapsManager.executeQuery(sql);
			@SuppressWarnings("rawtypes")
			HashMap hm = list.get(0);
			databaseAccess.close();
			return hm.get("OID").toString();
		}catch(Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	public List getOrder(String orders_id) {
		// TODO Auto-generated method stub
		String sql = "select * from `order` o where o.`orders_id`=?;";
		try{
			DatabaseAccess databaseAccess = new DatabaseAccess(false);
			EasyMapsManager easyMapsManager = new EasyMapsManager(databaseAccess);
			easyMapsManager.setPreparedParameter(orders_id);
			@SuppressWarnings("rawtypes")
			List list = easyMapsManager.executeQuery(sql);
			databaseAccess.close();
			return list;
		}catch(Exception e) {
			e.printStackTrace();
		}
		return null;
	}
}
